/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.agentes;

import co.edu.unal.behaviours.CheckStockServer;
import co.edu.unal.behaviours.CreateStock;
import co.edu.unal.behaviours.OfferRequestsServer;
import co.edu.unal.behaviours.PurchaseOrdersServer;
import co.edu.unal.gui.BookSellerGui;
import co.edu.unal.inventory.Stock;
import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import java.util.HashMap;

/**
 *
 * @author raider
 */
public class BookSellerAgent extends Agent {
// The catalogue of books for sale (maps the title of a book to its price)
    private HashMap catalogue;
// The GUI by means of which the user can add books in the catalogue
    private BookSellerGui myGui;
    
    private Stock stock;
    
    
// Put agent initializations here
    @Override
    protected void setup() {
        // Create and show the GUI
        myGui = new BookSellerGui(this);
        myGui.showGui();
        //
        stock = new Stock(100,10,1);
        addBehaviour(new CreateStock(stock,myGui.getLog()));
        myGui.getLog().setText("Hello! Seller-agent " + getAID().getLocalName() + " is ready.");
        // Create the catalogue
        catalogue = new HashMap();
        
        if (getLocalName().equals("seller1")) {
            catalogue.put("Estudio en escarlata", 200);
        }
        addBehaviour(new CheckStockServer(catalogue, stock,myGui.getLog(),200,"book-seller", myGui.getLista()));
        // Add the behaviour serving requests for offer from buyer agents
        addBehaviour(new OfferRequestsServer(catalogue));
        // Add the behaviour serving purchase orders from buyer agents
        addBehaviour(new PurchaseOrdersServer(catalogue,"book-seller",myGui.getLog()));
        // Register the book-selling service in the yellow pages
        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName(getAID());
        ServiceDescription sd = new ServiceDescription();
        sd.setType("book-seller");
        sd.setName("JADE-book-trading");
        dfd.addServices(sd);
        try {
            DFService.register(this, dfd);
        } catch (FIPAException fe) {
            myGui.getLog().setText(myGui.getLog()+"\n"+"fe.getMessage() = " + fe.getMessage());
        }

    }
// Put agent clean-up operations here
    @Override
    protected void takeDown() {
        // Deregister from the yellow pages
        try {
            DFService.deregister(this);
        } catch (FIPAException fe) {
            myGui.getLog().setText(myGui.getLog()+"\n"+"fe.getMessage() = " + fe.getMessage());
        }

// Close the GUI
        myGui.dispose();
// Printout a dismissal message
        myGui.getLog().setText(myGui.getLog()+"\n"+"Seller-agent " + getAID().getLocalName() + " terminating.");
    }

    /**
     * This is invoked by the GUI when the user adds a new book for sale
     */
    public void updateCatalogue(final String title, final int price) {
        catalogue.put(title, new Integer(price));
//        addBehaviour(new OneShotBehaviour() {
//            @Override
//            public void action() {
//                catalogue.put(title, new Integer(price));
//            }
//        });
    }
    
    public Stock crearInventario(int qOpt/*Tamaño lote*/, int d_anual/*demanda anual*/, int lT/*tiempo reabastecimiento*/, int iS/*Tamaño invet. seguridad*/){
        Stock inv;
        inv = new Stock(qOpt,d_anual,lT,iS);
        if(inv.validaInventario()){
            myGui.getLog().setText(myGui.getLog()+"\n"+"inventario valido : max "+ inv.getInventarioMaximo());
        }else{
            myGui.getLog().setText(myGui.getLog()+"\n"+"inventario invalido");
            inv = crearInventario(qOpt+10,d_anual,lT,iS);
        }
        return inv;
    }

    public Stock getInventario() {
        return stock;
    }

    public void setInventario(Stock inventario) {
        this.stock = inventario;
    }
}
